package com.djf.eg.service;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.djf.eg.dao.UserInfoDao;
import com.djf.eg.entity.UserInfo;
import com.djf.eg.util.Constants.EgConstants;
import com.djf.eg.util.convert.Entity2Vo;
import com.djf.eg.vo.UserInfoVo;

@Service
public class AuthService {
//	
//	@Autowired
//	SysPermissionDao sysPermissionDao;
	
	@Autowired
	UserInfoDao userInfoDao;
	/**
     * 查询当前登录用户的权限等信息
     *
     * @return
     */
    public UserInfoVo getInfo() {
        //从session获取用户信息
        Session session = SecurityUtils.getSubject().getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute(EgConstants.SESSION_USER_INFO);
        if (userInfo == null) {
        	return null;
        } else {
        	UserInfo fullInfo = userInfoDao.getUserById(userInfo.getId());
            UserInfoVo userVo = Entity2Vo.userEntity2Vo(fullInfo);
//        	List<SysPermission> permissionList = sysPermissionRepository.getPermissionByUserId(userInfo.getId());
        	session.setAttribute(EgConstants.SESSION_USER_PERMISSION, userVo);
        	return userVo;
        }
//        JSONObject returnData = new JSONObject();
//        JSONObject userPermission = permissionService.getUserPermission(username);
//        session.setAttribute(EgConstants.SESSION_USER_PERMISSION, userPermission);
//        returnData.put("userPermission", userPermission);
//        return CommonUtil.successJson(returnData);
    }
    
    public boolean logout() {
        try {
            Subject currentUser = SecurityUtils.getSubject();
            currentUser.logout();
            return true;
        } catch (Exception e) {
        	return false;
        }
    }
}
